// @Desc:
// @CreateTime: 2020/11/3
package wx

import (
	"crypto/sha1"
	"fmt"
	"sort"
	"strings"

	"github.com/gin-gonic/gin"
	"mycrm.com/config"
	mlog "mycrm.com/mylog"
	"mycrm.com/utils"
)

// 验证签名是否正确
func CheckSignature(c *gin.Context) bool {
	// signature=286acfb2906179738dd6a5fbd95f2b47594b8a64&echostr=908399367801438205&timestamp=1604203481&nonce=779755379"
	signature := c.Query("signature")
	timestamp := c.Query("timestamp")
	nonce := c.Query("nonce")
	authCode := utils.Dec(config.Configs.AuthConf.WxToken)
	mysignSlice := []string{timestamp, nonce, authCode}
	sort.Strings(mysignSlice)
	mySignStr := strings.Join(mysignSlice, "")
	mySha1Sign := sha1.Sum([]byte(mySignStr))
	mySha1SignStr := fmt.Sprintf("%x", mySha1Sign)
	// 如果不相同的话就说明不是来之微信
	if mySha1SignStr != signature {
		mlog.Log.Infof("validate wx signature is failed, reqSign: %s, mySign: %s", signature, mySha1SignStr)
		return false
	}
	// // 用于微信配置验签返回的
	// c.Writer.Write([]byte(c.Query("echostr")))
	return true
}
